package com.atguigu.auth.utils;

import com.atguigu.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:MenuHelper
 * Package:com.atguigu.auth.utils
 * Description:
 *
 * @Author:小飞机
 * @Create：2023/5/15 - 10:00
 * @Version: v1.0
 */
public class MenuHelper {
    public static List<SysMenu> buildTree(List<SysMenu> sysMenusList) {
        //递归
        List<SysMenu> trees = new ArrayList<>();
        //所有菜单数据进行遍历
        for (SysMenu sysMenu :
                sysMenusList) {
            //递归入口
            if (sysMenu.getParentId().longValue()==0){
                trees.add(getChildren(sysMenu,sysMenusList));
            }
        }
        return trees;
    }

    public static SysMenu getChildren(SysMenu sysMenu,List<SysMenu> sysMenuList){
        sysMenu.setChildren(new ArrayList<SysMenu>());
        for (SysMenu it :sysMenuList)
        {
            //遍历所有菜单数据，判断id和paranoid对应关系
            if (sysMenu.getId().longValue()==it.getParentId()) {
//                if (sysMenu.getChildren() == null) {
//                    sysMenu.setChildren(new ArrayList<>());
//                }
                sysMenu.getChildren().add(getChildren(it, sysMenuList));
            }
        }
        //遍历所有菜单数据，判断id和paranoid对应关系
        return sysMenu;
    }
}
